home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-06 | 3.0 KB | 130 lines | [TEXT/MSET] |
- (*
-
- Class $x is a dictionary-based simple string class whose length may vary,
- up to a maximum of 255, but the maximum length is defined at instantiation.
- We cheat a bit here and use Mops' INDEXED class definition abilities and
- indexed ivar data area in a way that was not really intended.
-
- $x's are nice for use as string ivars, or if you want a persistent string
- object in the dictionary (no handles here so we don't need to do a new:
- and restore the data at each runtime).
-
- in class $x we take advantage of the fact that the 2-byte Width field for
- indexed objects can be used for other storage *if* we are careful.
- Since the Width field is only really needed here at instantiation (we are
- careful not to use words that rely on Width actually being the width), we
- use it here to store the maximum length, or limit, of text in the $x.
- Also, we now use the byte just prior to the indexed data area to store
- the length of the text, so it is easy to obtain a str255 format string
- since all we need do is obtain this address (which is idxbase - 1 , see
- get$: ).
-
- Note that there are still 4 unused bytes that might be used for pos and lim
- as in string. I guess we are safe doing this until Michael changes the
- internal structure of indexed objects. (!!)
-
- *)
-
- :class $x super{ object } 1 indexed \ each character is one byte
- \ at instantiation we simply declare the maximum number of characters desired
-
- :m limit: ( -- lim )
- idxbase 6 - w@ ;m
-
- :m get$: \ ( -- $ptr ) \ str255 format
- idxbase 1 - ;m
-
- :m size: \ ( -- len)
- get$: self c@ ;m
-
- private \ private because we should never do this directly
-
- :m setsize: \ ( len --)
- dup limit: self > abort" No more room in $x."
- get$: self c!
- ;m
- public
-
- :m clear:
- 0 setsize: self ;m
-
- :m classinit:
- limit idxbase 6 - w! \ this must be the only time we set this
- clear: self ;m
-
- :m addr: \ ( -- addr) \ redefine to give us the indexed data area
- \ which will be the address of the first character of text
- idxbase ;m
-
- :m put: { addr len -- }
- len setsize: self
- addr ( src) addr: self ( dest) len ( cnt) cmove ;m
-
- :m get: ( -- addr len )
- addr: self size: self ;m
-
- :m print:
- get: self type ;m
-
- :m put$: { $ptr -- }
- $ptr 1 + $ptr c@ put: self ;m
-
- :m add: { addr len \ $len -- }
- size: self -> $len
- len $len + setsize: self
- addr ( src) addr: self $len + ( dest) len ( cnt) cmove ;m
-
- :m add$: { $ptr -- }
- $ptr 1 + $ptr c@ add: self ;m
-
- :m uc: ( -- ) \ converts to upper case
- get: self upper ;m
-
- :m +: ( c -- ) \ appends a char to the end of the string
- buf255 c! buf255 1 add: self ;m
-
- :m clip: { n -- } \ remove n characters from end of string
- \ if n is too large, string is just cleared with no error
- size: self n - 0 max setsize: self ;m
-
- ;class
-
- endload
-
-
- \ **** EXAMPLE USE
-
- 7 $x jj
-
- dump: jj
-
- print: jj
- size: jj .
- 3 clip: jj
- " hello" put: jj
- " ff" add: jj
- uc: jj print: jj
- 32 +: jj
-
- :class test super{ object }
- var junk
- 32 $x theString
- var morejunk
-
- :m classinit:
- 333 put: junk
- " Hello World " put: theString
- 666 put: morejunk ;m
-
- :m dump:
- get: junk .
- print: theString
- get: morejunk . ;m
-
- ;class
-
-
- test yy
-
- dump: yy
-